package com.qys.erp.service.impl;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qys.erp.domain.Product;
import com.qys.erp.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.qys.erp.domain.DeliveryLog;
import com.qys.erp.mapper.DeliveryMapper;
import com.qys.erp.domain.Delivery;
import com.qys.erp.service.IDeliveryService;

/**
 * 产品出库Service业务层处理
 * 
 * @author qys
 * @date 2021-08-22
 */
@Service
public class DeliveryServiceImpl implements IDeliveryService 
{
    @Autowired
    private DeliveryMapper deliveryMapper;

    @Autowired
    private ProductMapper productMapper;
    private Object Class;

    /**
     * 查询产品出库
     * 
     * @param delId 产品出库ID
     * @return 产品出库
     */
   // @Override
   // public Delivery outselectDeliveryById(Long delId){return deliveryMapper.outselectDeliveryById(delId);}

    /**
     * 查询产品出库
     *
     * @param delId 产品出库ID
     * @return 产品出库
     */
    @Override
    public Delivery selectDeliveryById(Long delId){return deliveryMapper.selectDeliveryById(delId);}

    /**
     * 查询产品出库列表
     *
     * @param delivery 产品出库
     * @return 产品出库
     */
    @Override
    public List<Delivery> outselectDeliveryList(Delivery delivery)
    {
        return deliveryMapper.outselectDeliveryList(delivery);
    }

    /**
     * 查询产品出库列表
     *
     * @param delivery 产品出库
     * @return 产品出库
     */
    @Override
    public List<Delivery> selectDeliveryList(Delivery delivery)
    {
        return deliveryMapper.selectDeliveryList(delivery);
    }

    /**
     * 新增产品出库
     * 
     * @param delivery 产品出库
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDelivery(Delivery delivery)
    {
        System.out.println("DDDDDDDDD"+delivery);
        int rows = deliveryMapper.insertDelivery(delivery);
        insertDeliveryLog(delivery);

        //修改库存数量
        final List<DeliveryLog> deliveryLogList = delivery.getDeliveryLogList();
        if (StringUtils.isNotNull(deliveryLogList))
        {
            //定义数组，根据产品pro_id修改库存
            List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
            for (DeliveryLog listCount : deliveryLogList)
            {
                Map params = new HashMap();
                if(delivery.getActionType() == 0){
                    //出库减库存
                    params.put("proNumber", Integer.parseInt(String.valueOf(productMapper.selectProductById(listCount.getProductId()).getProNumber() - listCount.getLogCount())));
                }else{
                    //入库加库存
                    params.put("proNumber", Integer.parseInt(String.valueOf(productMapper.selectProductById(listCount.getProductId()).getProNumber() + listCount.getLogCount())));
                }

                params.put("proId", Integer.parseInt(String.valueOf(listCount.getProductId())));
                list.add(params);
            }
            if (list.size() > 0)
            {
                //库存数量更新
                deliveryMapper.batchProduct(list);
            }
        }
        return rows;
    }

    /**
     * 修改产品出库
     * 
     * @param delivery 产品出库
     * @return 结果
     */
    @Transactional  //事务操作
    @Override
    public int updateDelivery(Delivery delivery)
    {
        //需要改成软删除，即把状态改为0，后期处理
        deliveryMapper.deleteDeliveryLogByDelId(delivery.getDelId());
        List<DeliveryLog> deliveryLogList = delivery.getDeliveryLogList();
        if (StringUtils.isNotNull(deliveryLogList))
        {
            //定义数组，根据产品pro_id修改库存
            List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
            for (DeliveryLog listCount : deliveryLogList)
            {
                Map params = new HashMap();
                long proId =  Integer.parseInt(String.valueOf(listCount.getProductId()));
                if(delivery.getActionType() == 0){
                    //出库减库存
                    params.put("proNumber", Integer.parseInt(String.valueOf(productMapper.selectProductById(listCount.getProductId()).getProNumber() - listCount.getLogCount())));
                }else{
                    //入库加库存
                    params.put("proNumber", Integer.parseInt(String.valueOf(productMapper.selectProductById(listCount.getProductId()).getProNumber() + listCount.getLogCount())));
                }

                params.put("proId", proId);
                list.add(params);
            }
            if (list.size() > 0)
            {
                //库存数量更新
               deliveryMapper.batchProduct(list);
            }
        }
        //更新出入库记录表
        insertDeliveryLog(delivery);
        return deliveryMapper.updateDelivery(delivery);
    }

    /**
     * 批量删除产品出库
     * 
     * @param delIds 需要删除的产品出库ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDeliveryByIds(Long[] delIds)
    {
        deliveryMapper.deleteDeliveryLogByDelIds(delIds);
        return deliveryMapper.deleteDeliveryByIds(delIds);
    }

    /**
     * 删除产品出库信息
     * 
     * @param delId 产品出库ID
     * @return 结果
     */
    @Override
    public int deleteDeliveryById(Long delId)
    {
        deliveryMapper.deleteDeliveryLogByDelId(delId);
        return deliveryMapper.deleteDeliveryById(delId);
    }

    /**
     * 新增出库记录信息
     * 
     * @param delivery 产品出库对象
     */
    public void insertDeliveryLog(Delivery delivery)
    {
        List<DeliveryLog> deliveryLogList = delivery.getDeliveryLogList();
        Long delId = delivery.getDelId();
        if (StringUtils.isNotNull(deliveryLogList))
        {
            List<DeliveryLog> list = new ArrayList<DeliveryLog>();
            for (DeliveryLog deliveryLog : deliveryLogList)
            {
                deliveryLog.setDelId(delId);
                list.add(deliveryLog);
            }
            if (list.size() > 0)
            {
                deliveryMapper.batchDeliveryLog(list);
            }
        }
    }
}
